package multithreading.deadlock.g2;

public class Pipeline {

	private Object data = null;

	public void put(Object newData) {
		synchronized (this) {
			System.out.println("Producer " + Thread.currentThread().getName()
					+ " entered put()...");
			while (data != null)
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			data = newData;
			System.out.println("Producer " + Thread.currentThread().getName()
					+ " put data: " + data);
			this.notifyAll();
		}
	}

	public synchronized Object get() {
		System.out.println("Consumer entered get()...");
		while (data == null)
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		Object theData = data;
		data = null;
		System.out.println("Consumer retrieved data: " + theData);
		this.notifyAll();
		return theData;
	}
}
